﻿//加载类型下拉框
function CreateTypeSelectOption(SelectedItem) {
    var selectObj = document.getElementById("select_Type_All");
    var selectType_AddUrlObj = document.getElementById("selectType_Url");

    selectObj.options.length = 0;
    selectType_AddUrlObj.options.length = 0;

    var Type_AllID_String = localStorage.getItem("Type_AllID_String");
    var Type_AllID_Array = Type_AllID_String.split(",");
    for (var i = 0, j = Type_AllID_Array.length; i < j; i++) {
        var Type_Name_Key = "Type_Name_" + Type_AllID_Array[i];
        var Type_Name_Value = localStorage.getItem(Type_Name_Key);
        var NewOption = new Option(Type_Name_Value, Type_AllID_Array[i]);
        NewOption.title = Type_Name_Value;
        if (Type_AllID_Array[i] == SelectedItem) {
            NewOption.selected = true;
        }
        selectObj.options.add(NewOption);
        selectType_AddUrlObj.options.add(new Option(Type_Name_Value, Type_AllID_Array[i]));
    }
}
//加载链接下拉框
function CreateUrlListSelectOption(SelectedItem) {
    var selectObj = document.getElementById("select_URL_List");

    selectObj.innerHTML = "";

    var Type_AllID_String = localStorage.getItem("Type_AllID_String");
    var Type_AllID_Array = Type_AllID_String.split(",");

    var SelectGropuOptionDoc = document.createDocumentFragment();
    for (var i = 0, j = Type_AllID_Array.length; i < j; i++) {
        var Type_Name_Key = "Type_Name_" + Type_AllID_Array[i];
        var Type_Name_Value = localStorage.getItem(Type_Name_Key);

        var GroupObj = document.createElement("optgroup");
        GroupObj.label = Type_Name_Value;
        GroupObj.id = "optgroup_" + Type_AllID_Array[i];

        var Type_URLIDS_Key = "Type_URLIDS_" + Type_AllID_Array[i];
        var Type_URLIDS_Value = localStorage.getItem(Type_URLIDS_Key);
        //新增加的类别这个值为null
        if (Type_URLIDS_Value == null) {
            continue;
        }
        var Type_URLIDS_Array = Type_URLIDS_Value.split(",");

        for (var k = 0, l = Type_URLIDS_Array.length; k < l; k++) {
            var URL_Text_Key = "URL_Text_" + Type_URLIDS_Array[k];
            var URL_Text_Value = localStorage.getItem(URL_Text_Key);

            var URL_URL_Key = "URL_URL_" + Type_URLIDS_Array[k];
            var URL_URL_Value = localStorage.getItem(URL_URL_Key);

            var NewOption = document.createElement("option");
            NewOption.value = Type_URLIDS_Array[k];
            NewOption.innerHTML = URL_Text_Value;
            NewOption.title = URL_URL_Value;
            if (Type_URLIDS_Array[k] == SelectedItem) {
                NewOption.selected = true;
            }
            //var NewOption = new Option(URL_Text_Value, Type_URLIDS_Array[k]);
            //NewOption.title = URL_URL_Value;

            GroupObj.appendChild(NewOption);
        }
        SelectGropuOptionDoc.appendChild(GroupObj);
    }
    selectObj.appendChild(SelectGropuOptionDoc);
}





/**********************************链接*****************************************/
var div_Modify_URL_Element;
function btnURLModify_AddClick() { //增加新的链接
    div_Modify_URL_Element = $.dialog({
        content: document.getElementById("div_Modify_URL"),
        title: "增加链接",
        ok: function () {
            var selectTypeID = document.getElementById("selectType_Url").value;
            var URLName = document.getElementById("txtURLName").value;
            var URLAddress = document.getElementById("txtURLAddress").value;
            if (URLName == "" || URLAddress == "") {
                alert("链接名称和链接地址不能为空!!!");
                return true;
            }
            var NewURLID = AddNewURLNode(selectTypeID, URLName, URLAddress);
            //CreateTypeSelectOption();
            CreateUrlListSelectOption(NewURLID);

            document.getElementById("txtURLName").value = "";
            document.getElementById("hidURLID").value = "";
            document.getElementById("txtURLAddress").value = "";
        },
        cancel: function () {
            document.getElementById("txtURLName").value = "";
            document.getElementById("hidURLID").value = "";
            document.getElementById("txtURLAddress").value = "";
        },
        okValue: "保存",
        cancelValue: "取消",
        lock: true,
        initialize: function () {
            document.getElementById("txtURLName").value = "";
            document.getElementById("hidURLID").value = "";
            document.getElementById("txtURLAddress").value = "";
        }
    });
}
function btnURLModify_ModifyClick() { //修改链接
    div_Modify_URL_Element = $.dialog({
        content: document.getElementById("div_Modify_URL"),
        title: "修改链接",
        ok: function () {
            var URLName = document.getElementById("txtURLName").value;
            var URLAddress = document.getElementById("txtURLAddress").value;
            var URLID = document.getElementById("hidURLID").value;
            if (URLName == "" || URLAddress == "" || URLID == "") {
                alert("链接名称和链接地址不能为空!!!");
                return true;
            }

            var URL_Text_Key = "URL_Text_" + URLID;
            localStorage.setItem(URL_Text_Key, URLName);
            var URL_URL_Key = "URL_URL_" + URLID;
            localStorage.setItem(URL_URL_Key, URLAddress);

            CreateUrlListSelectOption(URLID);

            document.getElementById("txtURLName").value = "";
            document.getElementById("hidURLID").value = "";
            document.getElementById("txtURLAddress").value = "";
            document.getElementById("txtURLAddress").disabled = false;
        },
        cancel: function () {
            document.getElementById("txtURLName").value = "";
            document.getElementById("hidURLID").value = "";
            document.getElementById("txtURLAddress").value = "";
        },
        okValue: "保存",
        cancelValue: "取消",
        lock: true,
        initialize: function () {
            var select_URL_ListObj = document.getElementById("select_URL_List");
            var Select_URL_Value = select_URL_ListObj.value;
            if (Select_URL_Value == "") {
                alert("请选择要修改的链接!!!");
                this.close();
            }
            var Select_URL_Text = select_URL_ListObj.options[select_URL_ListObj.selectedIndex].text;

            var URL_ParentID_Key = "URL_ParentID_" + Select_URL_Value;
            var URL_ParentID_Value = localStorage.getItem(URL_ParentID_Key);
            var selectType_UrlObj = document.getElementById("selectType_Url");
            selectType_UrlObj.value = URL_ParentID_Value;
            selectType_UrlObj.disabled = true;

            var URL_URL_Key = "URL_URL_" + Select_URL_Value;
            var URL_URL_Value = localStorage.getItem(URL_URL_Key);
            document.getElementById("txtURLName").value = Select_URL_Text;
            document.getElementById("hidURLID").value = Select_URL_Value;
            document.getElementById("txtURLAddress").value = URL_URL_Value;
            if (URL_URL_Value == "Edit.html") {
                document.getElementById("txtURLAddress").disabled = true;
            }
        }
    });
}
function btnURLModify_DeleteClick() { //删除一个现有链接
    var select_URL_ListObj = document.getElementById("select_URL_List");
    var Select_URL_Value = select_URL_ListObj.value;
    if (Select_URL_Value == "") {
        alert("请选择要删除的链接!!!");
        return;
    }

    var Select_URL_Text = select_URL_ListObj.options[select_URL_ListObj.selectedIndex].text;
    if (!confirm("你确定删除\"" + Select_URL_Text + "\"这个链接吗?")) {
        return;
    }

    var URL_Text_Key = "URL_Text_" + Select_URL_Value;
    var URL_URL_Key = "URL_URL_" + Select_URL_Value;
    var URL_URL_Value = localStorage.getItem(URL_URL_Key);
    if (URL_URL_Value == "Edit.html") {
        alert("不能删除管理链接.");
        return;
    }
    localStorage.removeItem(URL_Text_Key);
    localStorage.removeItem(URL_URL_Key);

    var URL_ParentID_Key = "URL_ParentID_" + Select_URL_Value;
    var URL_ParentID_Value = localStorage.getItem(URL_ParentID_Key);
    localStorage.removeItem(URL_ParentID_Key);

    var Type_URLIDS_Key = "Type_URLIDS_" + URL_ParentID_Value;
    var Type_URLIDS_Value = localStorage.getItem(Type_URLIDS_Key);
    Type_URLIDS_Value = "," + Type_URLIDS_Value + ",";
    Type_URLIDS_Value = Type_URLIDS_Value.replace("," + Select_URL_Value + ",", ",");
    if (Type_URLIDS_Value == ",") {//如果当前类别只有这一个.就把这个键删除
        localStorage.removeItem(Type_URLIDS_Key);
    } else {
        Type_URLIDS_Value = Type_URLIDS_Value.substring(1, Type_URLIDS_Value.length - 1);
        localStorage.setItem(Type_URLIDS_Key, Type_URLIDS_Value);
    }

    CreateUrlListSelectOption("");
}
function btnURLModify_MoveUpClick() {//向上移动链接
    var select_URL_ListObj = document.getElementById("select_URL_List");
    var Select_URL_Value = select_URL_ListObj.value;
    if (Select_URL_Value == "") {
        alert("请选择要移动的链接!!!");
        return;
    }
    var URL_ParentID_Key = "URL_ParentID_" + Select_URL_Value;
    var Type_URLIDS_Key = "Type_URLIDS_" + localStorage.getItem(URL_ParentID_Key);
    var Type_URLIDS_Value = localStorage.getItem(Type_URLIDS_Key);
    Type_URLIDS_Value = fun_Common_MoveUp(Type_URLIDS_Value, Select_URL_Value);
    localStorage.setItem(Type_URLIDS_Key, Type_URLIDS_Value);

    CreateUrlListSelectOption(Select_URL_Value);
}
function btnURLModify_MoveTypeClick() { //从一个分类移动到另一个分类
    var select_URL_ListObj = document.getElementById("select_URL_List");
    var Select_URL_Value = select_URL_ListObj.value;
    if (Select_URL_Value == "") {
        alert("请选择要移动的链接!!!");
        return;
    }
    var selectedTypeObj = document.getElementById("select_Type_All");
    var Select_Type_Value = selectedTypeObj.value;
    if (Select_Type_Value == "") {
        alert("把当前链接移动到哪个分类?请选择...");
        return;
    }
    var URL_ParentID_Key = "URL_ParentID_" + Select_URL_Value;
    var URL_ParentID_Value = localStorage.getItem(URL_ParentID_Key);
    if (URL_ParentID_Value == Select_Type_Value) {
        alert("当前链接已经在这个分类,请选择另一个分类.");
        return;
    }
    localStorage.setItem(URL_ParentID_Key, Select_Type_Value);
    var Type_URLIDS_Key = "Type_URLIDS_" + URL_ParentID_Value;
    var Type_URLIDS_Value = localStorage.getItem(Type_URLIDS_Key);
    Type_URLIDS_Value = "," + Type_URLIDS_Value + ",";
    Type_URLIDS_Value = Type_URLIDS_Value.replace("," + Select_URL_Value + ",", ",");
    if (Type_URLIDS_Value == ",") {//如果只有这一个链接.就把这个键删除
        localStorage.removeItem(Type_URLIDS_Key);
    } else {
        Type_URLIDS_Value = Type_URLIDS_Value.substring(1, Type_URLIDS_Value.length - 1);
        localStorage.setItem(Type_URLIDS_Key, Type_URLIDS_Value);
    }

    var Type_URLIDS_NewKey = "Type_URLIDS_" + Select_Type_Value;
    var Type_URLIDS_NewValue = localStorage.getItem(Type_URLIDS_NewKey);
    if (Type_URLIDS_NewValue == null) {
        Type_URLIDS_NewValue = Select_URL_Value;
    } else {
        Type_URLIDS_NewValue = Type_URLIDS_NewValue + "," + Select_URL_Value;
    }
    localStorage.setItem(Type_URLIDS_NewKey, Type_URLIDS_NewValue);

    CreateUrlListSelectOption(Select_URL_Value);
}










/**************************类别*****************************/
var div_Modify_Type_Element;
function btnTypeModify_AddClick() {//增加新的类别
    div_Modify_Type_Element = $.dialog({
        content: document.getElementById("div_Modify_Type"),
        title: "增加类别",
        ok: function () {
            var NewTypeName = document.getElementById("txtTypeName").value;
            if (NewTypeName == "") {
                this.content("名称不能为空!!!!");
                return true;
            }
            var NewTypeNameID = AddNewTypeNode(NewTypeName);

            CreateTypeSelectOption(NewTypeNameID);
            CreateUrlListSelectOption("");

            document.getElementById("txtTypeName").value = "";
            document.getElementById("hidTypeID").value = "";

            return true;
        },
        cancel: function () {
            document.getElementById("txtTypeName").value = "";
            document.getElementById("hidTypeID").value = "";
        },
        okValue: "保存",
        cancelValue: "取消",
        lock: true,
        initialize: function () {
            document.getElementById("txtTypeName").value = "";
            document.getElementById("hidTypeID").value = "";
        }
    });
}
function btnTypeModify_EditClick() {//修改现有类别
    div_Modify_Type_Element = $.dialog({
        content: document.getElementById("div_Modify_Type"),
        title: "修改类别",
        ok: function () {
            var NewTypeName = document.getElementById("txtTypeName").value;
            var TypeNameID = document.getElementById("hidTypeID").value;
            if (NewTypeName == "") {
                this.content("名称不能为空!!!!");
                return true;
            }
            var TypeNameKeyString = "Type_Name_" + TypeNameID;
            localStorage.setItem(TypeNameKeyString, NewTypeName);

            CreateTypeSelectOption(TypeNameID);
            CreateUrlListSelectOption("");

            document.getElementById("txtTypeName").value = "";
            document.getElementById("hidTypeID").value = "";
        },
        cancel: function () {
            document.getElementById("txtTypeName").value = "";
            document.getElementById("hidTypeID").value = "";
        },
        okValue: "保存",
        cancelValue: "取消",
        lock: true,
        initialize: function () {
            var selectedTypeObj = document.getElementById("select_Type_All");
            if (selectedTypeObj.value == "") {
                alert("请选择要修改的类别!!!!!!");
                this.close();
            } else {
                document.getElementById("hidTypeID").value = selectedTypeObj.value;
                document.getElementById("txtTypeName").value = selectedTypeObj.options[selectedTypeObj.selectedIndex].text;
            }
        }
    });
}
function btnTypeModify_DeleteClick() { //删除一个现有类别
    var selectedTypeObj = document.getElementById("select_Type_All");
    if (selectedTypeObj.value == "") {
        alert("请选择要删除的类别!!!!!!");
        return;
    }
    var SelectTypeID = selectedTypeObj.value;
    var Type_Name_Key = "Type_Name_" + SelectTypeID;
    if (!confirm("确定删除\"" + localStorage.getItem(Type_Name_Key) + "\"类别吗?")) {
        return;
    }
    var Type_URLIDS_Key = "Type_URLIDS_" + SelectTypeID;
    var Type_URLIDS_Value = localStorage.getItem(Type_URLIDS_Key);
    if (Type_URLIDS_Value != null) {
        alert("请先删除此类别下面的链接!!!!!!");
        return;
    }
    localStorage.removeItem(Type_URLIDS_Key);
    localStorage.removeItem(Type_Name_Key);
    //从主字条串内删除这个类别
    var Type_AllID_String_Value = localStorage.getItem("Type_AllID_String");
    var Type_AllID_String_NewValue = "," + Type_AllID_String_Value + ",";
    Type_AllID_String_NewValue = Type_AllID_String_NewValue.replace("," + SelectTypeID + ",", ",");
    Type_AllID_String_NewValue = Type_AllID_String_NewValue.substring(1, Type_AllID_String_NewValue.length - 1);
    localStorage.setItem("Type_AllID_String", Type_AllID_String_NewValue);

    CreateTypeSelectOption("");
    CreateUrlListSelectOption("");
}

function btnTypeModify_UpClick() { //向上移动一个类别
    var selectedTypeObj = document.getElementById("select_Type_All");
    if (selectedTypeObj.value == "") {
        alert("请选择要移动的类别!!!!!!");
        return;
    }
    var SelectTypeID = selectedTypeObj.value;
    var Type_AllID_String = localStorage.getItem("Type_AllID_String");

    Type_AllID_String = fun_Common_MoveUp(Type_AllID_String, SelectTypeID);

    localStorage.setItem("Type_AllID_String", Type_AllID_String);

    CreateTypeSelectOption(SelectTypeID);
    CreateUrlListSelectOption("");
}
function fun_Common_MoveUp(AllString, MoveString) {
    //把一个字符串从主串中向前移动一步
    AllString = "," + AllString + ",";
    MoveString = "," + MoveString + ",";
    var TypeIDIndex = AllString.indexOf(MoveString, 0);
    if (TypeIDIndex != 0) {
        AllString = AllString.replace(MoveString, ",");
        var NewTypeIDIndex = AllString.lastIndexOf(",", TypeIDIndex - 1);
        AllString = AllString.substring(0, NewTypeIDIndex) + MoveString + AllString.substring(NewTypeIDIndex + 1);
    }
    AllString = AllString.substring(1, AllString.length - 1);
    return AllString;
}







/********************************************************************************************/
//增加一个新的类别
function AddNewTypeNode(TypeName) {
    var id = fun_Common_NewGUID();
    var TypeNameKeyString = "Type_Name_" + id;
    //保存类型名称:前加"Type_Name_"
    localStorage.setItem(TypeNameKeyString, TypeName);

    //        var TypeURLIDKeyString = "Type_URLIDS_" + id;
    //        localStorage.setItem(TypeURLIDKeyString, "");

    var Type_AllID_String = localStorage.getItem("Type_AllID_String");
    Type_AllID_String += "," + id;
    localStorage.setItem("Type_AllID_String", Type_AllID_String);

    return id;
}
//增加一个新的链接
function AddNewURLNode(TypeID, URLName, URLAddress) {
    var id = fun_Common_NewGUID();
    var URL_Text_Key = "URL_Text_" + id;
    localStorage.setItem(URL_Text_Key, URLName);
    var URL_URL_Key = "URL_URL_" + id;
    localStorage.setItem(URL_URL_Key, URLAddress);
    var URL_ParentID_Key = "URL_ParentID_" + id;
    localStorage.setItem(URL_ParentID_Key, TypeID);

    var Type_URLIDS_Key = "Type_URLIDS_" + TypeID;
    var Type_URLIDS_Value = localStorage.getItem(Type_URLIDS_Key);
    if (Type_URLIDS_Value == null) {
        Type_URLIDS_Value = id;
    } else {
        Type_URLIDS_Value = Type_URLIDS_Value + "," + id;
    }
    localStorage.setItem(Type_URLIDS_Key, Type_URLIDS_Value);

    return id;
}